﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using NopSolutions.NopCommerce.BusinessLogic.Data;
using NopSolutions.NopCommerce.BusinessLogic.Model;

namespace NopSolutions.NopCommerce.BusinessLogic.Services
{
    public static class CategoryCompiledQueries
    {
        public static readonly Func<NopObjectContext, int, Category> CategoryById =
            CompiledQuery.Compile<NopObjectContext, int, Category>(
                (ctx, id) =>
                    ctx.Categories.FirstOrDefault(category => category.CategoryId == id)
            );

        public static readonly Func<NopObjectContext, int, bool, IQueryable<Category>> GetAllCategoriesByParentCategoryId =
            CompiledQuery.Compile<NopObjectContext, int, bool, IQueryable<Category>>(
                (ctx, parentCategoryId, showHidden) => ctx.Categories.Where(c=> (showHidden || c.Published) && !c.Deleted && c.ParentCategoryId == parentCategoryId).OrderBy(c=>c.DisplayOrder)
            );
    }
}
